package com.pshcp.controller;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.domain.dto.SysDictDTO;
import com.pshcp.domain.model.SysDict;
import com.pshcp.domain.vo.SysDictVO;
import com.pshcp.service.SysDictService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author auto generator
 * @since 2022-11-22
 */
@ApiIgnore
@RestController
@RequestMapping("/api/sysDict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @ApiOperation("列表")
    @Log(source = LogSourceEnum.ADMIN, type = LogTypeEnum.SELECT, desc = "列表")
    @GetMapping("list")
    public ResultDTO<PageList<SysDictVO>> list(SysDictDTO sysDictDTO){
        PageList<SysDict> pageList = sysDictService.pageList(sysDictDTO);
        return ResultBuilder.success(CommonTool.buildPageList(pageList, SysDictVO.class));
    }

    @ApiOperation("详情")
    @Log(source = LogSourceEnum.ADMIN, type = LogTypeEnum.SELECT, desc = "详情")
    @GetMapping("info/{id}")
    public ResultDTO<SysDictVO> info(@PathVariable Long id){
        SysDict sysDict = sysDictService.findById(id);
        return ResultBuilder.success(CommonTool.copyProperties(sysDict, SysDictVO.class));
    }

    @ApiOperation("新增")
    @Log(source = LogSourceEnum.ADMIN, type = LogTypeEnum.ADD, desc = "新增")
    @PostMapping("info")
    public ResultDTO add(@RequestBody SysDictDTO sysDictDTO){
        sysDictService.insert(CommonTool.copyProperties(sysDictDTO, SysDict.class));
        return ResultBuilder.success();
    }

    @ApiOperation("修改")
    @Log(source = LogSourceEnum.ADMIN, type = LogTypeEnum.UPDATE, desc = "修改")
    @PutMapping("info/{id}")
    public ResultDTO modify(@PathVariable Long id, @RequestBody SysDictDTO sysDictDTO) throws BizException {
        CommonTool.checkObj(sysDictDTO, "对象不能为空");
        CommonTool.checkObj(id, "ID不能为空");
        sysDictDTO.setId(id);
        sysDictService.update(CommonTool.copyProperties(sysDictDTO, SysDict.class));
        return ResultBuilder.success();
    }

    @ApiOperation("删除")
    @Log(source = LogSourceEnum.ADMIN, type = LogTypeEnum.LOGIC_DEL, desc = "删除")
    @DeleteMapping("info/{id}")
    public ResultDTO del(@PathVariable Long id){
        sysDictService.delById(id);
        return ResultBuilder.success();
    }

}

